import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime } from 'rxjs/operators/debounceTime';
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { StudentJoinCourseOutput } from '../../../core/model/StudentJoinCourseDto';
import { isNumber } from 'util';

@Component({
  selector: 'app-notice',
  templateUrl: './notice.component.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class NoticeComponent extends NgZorroPageBaseComponent implements OnInit {
  listUrl = 'ClassInfo/GetClassByName';
  searchChange$ = new BehaviorSubject(new SearchValue('', ''));
  optionList = [];
  selectedUser;
  // 开始演练
  startDrillId;
  confirmModal: NzModalRef;
  fwbBjq;
  messagedata;
  msgTitle = '';
  /**
   * 是否从远程获取数据
   */
  isLoading = false;
  type;

  onSearch(value: string, url: string): void {
    this.isLoading = true;
    this.searchChange$.next(new SearchValue(value, this.listUrl));
  }

  constructor(
    injecter: Injector,
    public sanitizer: DomSanitizer,
    private _message: NzMessageService,
    private modal: NzModalService,
    public httpService: HttpService,
    public http: HttpClient) {
    super(injecter,
      new BaseComponentConfig(
        "Notice/GetPagerNotice",
        "",
        "",
        "",
        "",
        "id",
        "",
        "通知管理",
        [],
        "Id",
        { Edit: false, Dell: false },
      ));
    this.listEntities.pageSize = 12;
  }
  async toAddNotice() {
    this.router.navigate(['app/noticeadd']);
  }
  async ngOnInit() {
    this.type = sessionStorage.getItem('userRoleType'); // 身份
  }

  // 详情
  async toDetail(value: any) {

  }
  // 编辑
  async editNotice(id) {
    this.router.navigate(['app/noticeedit', id]);
  }
  // 删除
  async deleteNotice(id) {

    this.confirmModal = this.modal.confirm({
      nzTitle: '是否要删除通知?',
      nzContent: '点击确定后，此通知的信息将被删除。',
      nzOnOk: async () => {
        const result = await this.httpService.get<any>('Notice/DeleteNotice?id=' + id);
        this.getEntityList();
      },
      nzOnCancel: () => {

      }
    });
  }



  keyupHandler(event) {
    this.fwbBjq = event;
  }

  async saveFeed() {
    if (!this.fwbBjq) {
      this._message.error("请填写通知内容");
    } else if (!this.msgTitle) {
      this._message.error("请填写标题");
    } else {
      const data = { "message": this.fwbBjq, title: this.msgTitle };
      await this.httpService.post<any>("SendNotifications/CreateMessage", data);
      this._message.success("发送成功");
    }
  }
  adminnotice() {
    this.router.navigate(['app/adminnotice'], {
      queryParams: {
        sm: "admin"
      }
    });
  }
  assembleHTML(strHTML: any) {
    return this.sanitizer.bypassSecurityTrustHtml(this.messagedata);
  }
}
